#!/bin/sh

# Slackware build script for glassfish

# Written by crocket (crockabiscuit@gmail.com)

PRGNAM=glassfish
VERSION=${VERSION:-3.1.2}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

MAJORVER=$(echo $VERSION | cut -d. -f1)

ARCH=noarch

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

# Bail out if user or group isn't valid on your system
# For slackbuilds.org, assigned glassfish uid/gid are 263/263
# See http://slackbuilds.org/uid_gid.txt
if ! getent group glassfish 2>&1 > /dev/null; then
    echo "  You must have a \"glassfish\" group to run this script."
    echo "    # groupadd -g 263 glassfish"
    exit 1
elif ! getent passwd glassfish 2>&1 > /dev/null; then
    echo "  You must have a \"glassfish\" user to run this script."
    echo "    # useradd -u 263 -g glassfish -d /var/lib/glassfish -s /bin/false glassfish"
    exit 1
fi

rm -rf $PKG
mkdir -p $PKG $OUTPUT

mkdir -p $PKG/opt
unzip $CWD/$PRGNAM-$VERSION-ml.zip -d $PKG/opt

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
unzip $CWD/$PRGNAM-ose-$VERSION-docs-pdf.zip -d $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/usr/bin
sed "s/%APPNAME%/$PRGNAM$MAJORVER/" $CWD/asadmin > $PKG/usr/bin/asadmin
chmod +x $PKG/usr/bin/asadmin

mkdir -p $PKG/etc/rc.d
cat $CWD/rc.glassfish > $PKG/etc/rc.d/rc.glassfish.new

mkdir -p $PKG/var/lib/$PRGNAM

chown -R root:root $PKG
chown -R glassfish:glassfish $PKG/opt/$PRGNAM$MAJORVER $PKG/var/lib/$PRGNAM

find $PKG \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}

